package com.demo.code.start;

import com.demo.code.generate.config.DataSourceConfig;
import com.demo.code.generate.config.GlobalConfig;
import com.demo.code.generate.CodeGenerate;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* 代码生成器
*/
public class CodeGenerateStart {

    public void generateToFile() throws IOException {
        Properties env = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = CodeGenerateStart.class.getClassLoader().getResourceAsStream("application.properties");
        // 使用properties对象加载输入流
        env.load(in);

        //System.out.println(env);
        String templatePath = env.getProperty("codeGenerate.template.path");
        String templateType = env.getProperty("codeGenerate.template.type");
        String templateAuthor = env.getProperty("codeGenerate.template.author");
        String templateEntityPackage = env.getProperty("codeGenerate.template.package.entity");
        String templateMapperPackage = env.getProperty("codeGenerate.template.package.mapper");
        String templateMapperXmlPackage = env.getProperty("codeGenerate.template.package.mapper.xml");
        String templateServicePackage = env.getProperty("codeGenerate.template.package.service");
        String templateServiceImplPackage = env.getProperty("codeGenerate.template.package.service.impl");
        String templateControllerPackage = env.getProperty("codeGenerate.template.package.controller");

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        //自定义模板路径
        globalConfig.setTemplatepath(templatePath);
        globalConfig.setType(templateType);
        globalConfig.setAuthor(templateAuthor);
        //实体包名
        globalConfig.setEntityPackage(templateEntityPackage);
        //mapper包名
        globalConfig.setMapperPackage(templateMapperPackage);
        //mapper的xml路径
        globalConfig.setMapperXmlPath(templateMapperXmlPackage);
        //service包名
        globalConfig.setServicePackage(templateServicePackage);
        globalConfig.setServiceImplPackage(templateServiceImplPackage);
        globalConfig.setControllerPackage(templateControllerPackage);

        //输出路径
        String allTable = env.getProperty("codeGenerate.template.allTable");
        String outputDir = env.getProperty("codeGenerate.outputDir");
        String tableNames = env.getProperty("codeGenerate.template.tableNames");
        String[] tableName = tableNames.split(",");
        String prefix = env.getProperty("codeGenerate.template.prefix");
        String[] prefixs = prefix.split(",");
        String[] ruleOutTables = env.getProperty("codeGenerate.template.ruleOutTable").split(",");

        //是否仅仅执行选择的表
        globalConfig.setAllTable(allTable);
        //需要生成的实体
        globalConfig.setTableNames(tableName);
        //排除的表
        globalConfig.setRuleOutTables(ruleOutTables);
        //生成的实体移除前缀
        globalConfig.setPrefix(prefixs);
        //文件输出路径，不配置的话默认输出当前项目的resources/code目录下
        globalConfig.setOutputDir(outputDir);

        //数据库配置
        String dataSourceDriverName = env.getProperty("data.Source.driverName");
        String dataSourceUrl = env.getProperty("data.Source.url");
        String dataSourceDataName = env.getProperty("data.Source.dataName");

        String dataSourceUsername = env.getProperty("data.Source.username");
        String dataSourcePassword = env.getProperty("data.Source.password");

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName(dataSourceDriverName);
        dsc.setUrl(dataSourceUrl+"/"+dataSourceDataName+"?useUnicode=true");
        dsc.setDataName(dataSourceDataName);
        //填写自己的数据库账号
        dsc.setUsername(dataSourceUsername);
        //填写自己的数据库密码
        dsc.setPassword(dataSourcePassword);
        CodeGenerate codeGenerate = new CodeGenerate(globalConfig, dsc);
        //生成代码
        codeGenerate.generateToFile();
    }
}
